package cn.itcast.reggie.controller;

import cn.itcast.reggie.common.MyIllegalArgumentException;
import cn.itcast.reggie.common.R;
import cn.itcast.reggie.pojo.Category;
import cn.itcast.reggie.pojo.Dish;
import cn.itcast.reggie.pojo.Setmeal;
import cn.itcast.reggie.service.CategoryService;
import cn.itcast.reggie.service.DishService;
import cn.itcast.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
	
    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    @GetMapping("/page")
    public R page(@RequestParam("page") Integer currentPage,Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper<Category> queryWapper = new QueryWrapper<>();
        queryWapper.orderByAsc("sort");
        Page result = categoryService.page(page,queryWapper);
        return R.success(result);
    }

   /* @DeleteMapping
    public R<String> delete(Long id){
        categoryService.removeById(id);
        return R.success("分类信息删除成功");
    }*/

   @Autowired
   private DishService dishService;

   @Autowired
   private SetmealService setmealService;
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") Long id){
        //categoryService.removeById(id);
        //1.根据分类id查询分类对象；
        Category category = categoryService.getById(id);
        //2.判断该分类是菜品分类还是套餐分类；---->type
        if(category.getType() == 1) {
            //3.如果是菜品分类；
            //根据菜品分类查询菜品数量
            QueryWrapper<Dish> query = Wrappers.<Dish>query().eq("category_id", id);
            int count = dishService.count(query);
            //如果数量>=1 抛出异常；
            if(count >= 1) {
                throw new MyIllegalArgumentException("该菜品分类下面有菜品不能删除!");
            }
        }else if(category.getType() == 2) {
            //如果是套餐分类
            //根据套餐分类查询菜品数量
            QueryWrapper<Setmeal> query = Wrappers.<Setmeal>query().eq("category_id", id);
            int count = setmealService.count(query);
            //如果数量>=1 抛出异常；
            if(count >= 1) {
                throw new MyIllegalArgumentException("该套餐分类下面有套餐不能删除!");
            }
        }
        //4.根据id删除分类
        categoryService.removeById(id);
        //5.返回R
        return R.success("分类信息删除成功");
    }

    @PutMapping
    public R update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功!");
    }

    /**
     * 根据type查询分类(菜品分类,套餐分类)
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R list(Category category) {
        //条件构造器
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //添加条件
        if(category.getType() != null) {
            queryWrapper.eq("type",category.getType());
        }
        queryWrapper.orderByDesc("update_time");
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }


}    